package Test.D5;

/**
 * String 实践--协议解析
 */
public class D5String {
    public static void main(String[] args) {
        String[] test=
        {
            null,
            "",
            "Time:20221020#GPS:22.452,110.3456#end&",
            "@Time:20221020#GPS:22.452,110.3456",
            "@end&",
            "@123end&",
            "@123#end&",
            "@123Time:#end&",
            "@Time:20221020#GPS:22.452,110.3456#end&",
        };

        Data data = new Data();

        for(int i=0;i<test.length;i++)
        {
            System.out.println("\n ----开始测试数据 "+test[i]+"  ------");
            try {
                data.parse(test[i]);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("协议错误: "+e.getMessage());
            }
            System.out.println(" ----数据结束 "+test[i]+"  ------\n");
        }
    }
}

class Data
{
    public String mTime,mGps;

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "时间："+mTime+"  GPS数据是:"+mGps;
    }

    final char mStartType = '@';
    final char mMidType = '#';
    final String mEndType = "end&";
    final String mTimeType = "Time:";
    final String mGpsType = "GPS:";
    /**
     * 解析通讯协议
     * @param data
     * @throws Exception
     */
    public void parse(String data) throws Exception
    {
        if(data == null)
        {
            throw new Exception("错误:解析数据是空数据");
        }
        if(data.isEmpty())
        {
            throw new Exception("错误:数据长度为0");
        }
        if(data.charAt(0)!=mStartType)
        {
            throw new Exception("错误:数据起始符错误");
        }

        if(!data.endsWith(mEndType))
        {
            throw new Exception("错误:数据结束符错误");
        }

        //重新获得去除起始符 @ 和结束符 end& 的数据
        String tdata = data.substring(1, data.length()-mEndType.length());

        if(tdata.indexOf(mMidType)<0)
        {
            throw new Exception("错误:缺少数据间隔符");
        }

        //获取时间 0--第一个间隔 # 的数据
        String tTime = tdata.substring(0, tdata.indexOf(mMidType));
        if(tdata.indexOf(mTimeType)<0)
        {
            throw new Exception("错误:时间数据未定义");
        }
        if(tdata.indexOf(mTimeType)>0)
        {
            throw new Exception("错误:时间数据位置不对");
        }
        //去除时间之后的数据 +1 为间隔符的长度
        tdata = tdata.substring(tTime.length()+1);

        //去除时间比较字符 Time: 获得对应的时间
        tTime = tTime.substring(mTimeType.length());

        if(tdata.indexOf(mMidType)<0)
        {
            throw new Exception("错误:缺少数据间隔符");
        }

        //获取GPS的数据 0--间隔符 # 之间的数据
        String tGps = tdata.substring(0, tdata.indexOf(mMidType));

        if(tdata.indexOf(mGpsType)<0)
        {
            throw new Exception("错误:GPS数据未定义");
        }
        if(tdata.indexOf(mGpsType)>0)
        {
            throw new Exception("错误:GPS数据位置不对");
        }

        //去除 GPS 比较字符 GPS: 获得对应的GPS数据
        tGps = tGps.substring(mGpsType.length());

        mTime = tTime;
        mGps = tGps;

        System.out.println("@@协议解析完成@@ "+this);
    }

    
}
